﻿
function onSignMouseOver() {
    document.body.style.cursor = 'pointer';
}

function onSignMouseOut() {
    document.body.style.cursor = 'default';
}

function signupBack() {
    $("#divSignUp").slideUp();
    $("#divDestination").slideDown();
}

// Get airport code
function buildQuery(term) {
    return "select * from json where url = 'http://airportcode.riobard.com/search?fmt=JSON&q=" + term + "'";
}

// Airport code request
function makeRequest(request, response) {

    $.ajax({
        url: 'http://query.yahooapis.com/v1/public/yql',
        data: {
            q: buildQuery(request.term),
            format: "json"
        },
        dataType: "jsonp",
        success: function (data) {

            var airports = [];
            if (data && data.query && data.query.results && data.query.results.json && data.query.results.json.json) {
                airports = data.query.results.json.json;
            }

            response($.map(airports, function (item) {
                return {
                    label: item.name,
                    value: item.code
                    //label: item.code + (item.name ? ", " + item.location : "") + ", " + item.location,
                    //value: item.code
                };
            }));
        },
        error: function () {
            response([]);
        }
    });
}

// register validation
$(document).ready(function () {

    // Hookup destination event with autocomplete
    $("#txtDestination").autocomplete({
        source: makeRequest,
        minLength: 2
    });

    $("#btndestinationNext").click(function () {
        $(".signUpError").hide();
        var hasError = false;

        var destinationVal = $("#txtDestination")[0].value;
        if (destinationVal == '') {
            $("#txtDestination").after("<div id='divDestinationError' class='signUpError'>Please enter your destination.</div>");
            hasError = true;
        }

        if (hasError == true) {
            return false; // exist out
        }
        else {
            // show next information collection screen
            $("#divDestination").slideUp();
            $("#divSignUp").slideDown();
        }
    });


    $("#btncreateProfile").click(function () {
        $(".signUpError").hide();
        var hasError = false;
        var hasName = false;
        var hasEmail = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var nameVal = $("#txtName")[0].value;
        var emailVal = $("#txtEmail")[0].value;

        if (nameVal == '') {
            $("#txtName").after("<div id='divNameError' class='signUpError'>Please enter your name.</div>");
            hasError = true;
        }
        else {
            hasName = true;
        }

        if (emailVal == '') {
            $("#txtEmail").after("<div id='divEmailError' class='signUpError'>Please enter your email address.</div>");
            hasError = true;
        }
        else if (!emailReg.test(emailVal)) {
            $("#txtEmail").after("<div id='divEmailError' class='signUpError'>Please enter a valid email address.</div>");
            hasError = true;
        }
        else {
            hasEmail = true;
        }

        if (hasName) { $("#divNameError").remove(); }
        if (hasEmail) { $("#divEmailError").remove(); }

        if (hasError) {
            return false; // exist out
        }
        else {
            // call function from /Scripts/createProfile.js
            createProfile();
        }

    });
});